package com.jyh.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.jyh.util.JavaBeanForMap;
import com.jyh.util.Utils;

import net.sf.json.JSONArray;

/**
 * 
 * @ClassName: BaseAction
 * @Description: 其他Action都应该继承此类，BaseAction封装与页面数据交互的基本方法
 */
public class BaseAction {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@ModelAttribute
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	@ModelAttribute
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	protected HttpSession getSession() {
		return request.getSession();
	}


	/**
	 * 处理直接返回Json参数
	 * @param jsonObject
	 * @return
	 */
	protected String jsonOut(Object jsonObject) {
		String jsonString;
		try {
			jsonString = JSON.toJSONString(jsonObject, SerializerFeature.DisableCircularReferenceDetect);// 禁止循环引用
			HttpServletResponse response = getResponse();
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().print(jsonString);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 处理作为参数返回Json
	 * @param jsonObject
	 * @return
	 */
	protected String jsonOutSimplify(Object jsonObject) {
		return JSON.toJSONString(jsonObject);
	}
	
	/**
	 * json 转list<Map>封装
	 * @param json
	 * @return
	 */
	protected static List<Map> getJsonForListToMap(String json) {
		List<Map> list = new ArrayList<Map>();
		if (!Utils.isNull(json)) {
			list = (List<Map>) JavaBeanForMap.reflect(JSONArray.fromObject(json));
			return list;
		} else {

			return list;
		}

	}

	/**
	 * 
	 * @Title: subStr
	 * @Description: 根据length截取字符串（length值传入总长度即可）
	 * @param s
	 * @param length
	 * @return
	 * @return: String
	 */
	protected String subStr(String s, int length) {
		if (s != null && !"".equals(s)) {
			if (s.length() <= length) {
				return s;
			} else {
				return s.substring(0, length) + "...";

			}
		} else {
			return "";
		}
	}
	
	
	/**
	 * 拼接limit值
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	protected String limit(int page, int pageSize) {
		return " limit " + ((page - 1) * pageSize) + "," + pageSize;
	}

}
